package com.java.huxb.threadlocal;

import com.java.huxb.utils.ThreadUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * @author huxb
 * @date 2020/4/9 0009 下午 4:09
 */
public class ThreadLocal003 {
    public  String date(int seconds) {
        Date  date = new Date(1000 * seconds);
        SimpleDateFormat dateFormat = ThreadSafeFormatter.dateFormatThreadLocal2.get();
       return dateFormat.format(date);
    }

    public static void main(String[] args) {

        for (int i = 1; i < 1000; i++) {
            int ii = i;
            ThreadUtils.execute(() -> {
                String date = new ThreadLocal002().date(ii);

                System.out.println(date);

            });
        }
    }


}

class ThreadSafeFormatter {

    public static ThreadLocal<SimpleDateFormat> dateFormatThreadLocal = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };

    public static ThreadLocal<SimpleDateFormat> dateFormatThreadLocal2 = ThreadLocal
            .withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
}
